from flask import Flask, jsonify
import json
from flask_cors import CORS

app = Flask(__name__)
CORS(app)


@app.route('/')
def get_data():
    try:
        with open('../public/data.json', 'r',encoding='utf-8') as f:  # 修改文件路径
            data = json.load(f)
            print("Successfully read data.json")
            print(data)  # 打印文件内容
            return jsonify(data)  # 返回 JSON 数据
    except FileNotFoundError as e:
        print("File not found")
        print(str(e))  # 打印错误信息
        return jsonify({"error": "File not found"}), 404  # 返回错误信息和 404 状态码
    except json.JSONDecodeError as e:
        print("File is not valid JSON")
        print(str(e))  # 打印错误信息
        return jsonify({"error": "File is not valid JSON"}), 400  # 返回错误信息和 400 状态码


if __name__ == '__main__':
    app.run(port=8080, debug=True)  # Ch